package cn.java79.springboot.zsb.pojo;

import cn.java79.springboot.zsb.entity.User;

/**
 * @author oldliu
 * @since 1.0
 */
//for ajax
public class AjaxResult {
    private int code;//0：表示成功，其他值含义。。。404,500,403重复了。。。
    private String msg="操作成功";
    private Object data;//传递的数据

    public AjaxResult() {

    }

    public AjaxResult(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public AjaxResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    public AjaxResult(int code){
        this.code=code;
    }
    public static AjaxResult success() {
        return new AjaxResult(0);
    }

    public static AjaxResult success(Object data) {
        return new AjaxResult(0, null, data);
    }

    //根据data是否为空，判断是返回成功还是失败
    public static AjaxResult rtn(Object data) {
        if (data == null)
            return error();
        return success(data);
    }

    public static AjaxResult error() {
        return new AjaxResult(400, null);
    }

    public static AjaxResult error(String msg) {
        return new AjaxResult(400, msg);
    }

    public static AjaxResult error(int code, String msg) {
        return new AjaxResult(code, msg);
    }

    public static AjaxResult judge(Object user) {
        if(user==null)
            return error("对象没有找到");

        return success(user);
    }
    public static AjaxResult judge(boolean b) {
        if(!b)
            return error("操作失败");

        return success();
    }
    public int getCode() {
        return code;
    }

    public boolean isSuccess() {
        return code == 0;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
